package com.qxmly.fm.adapters;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.qxmly.fm.R;
import com.qxmly.fm.utils.LogUtil;
import com.squareup.picasso.Picasso;
import com.ximalaya.ting.android.opensdk.model.album.Album;

import java.util.ArrayList;
import java.util.List;

/**
 * Q
 * Date:2021/11/26$
 **/
public class RecommendAdapter extends RecyclerView.Adapter<RecommendAdapter.Innerholder> {
    private static final String TAG = "RecommendAdapter";
    private List<Album> mData =new ArrayList<>();
    private OnRecommendItemClick mItemClickListener=null;


    @NonNull
    @Override
    public RecommendAdapter.Innerholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemview = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recommend,parent,false);
        return new Innerholder(itemview);
    }

    @Override
    public void onBindViewHolder(@NonNull RecommendAdapter.Innerholder holder, int position) {
        holder.itemView.setTag(position);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mItemClickListener!=null){
                    int clickPosition=(int) v.getTag();
                    mItemClickListener.onItemClick(clickPosition, mData.get(clickPosition));
                }
                LogUtil.d(TAG,"itemview click"+v.getTag());

            }
        });
        holder.setData(mData.get(position));

    }

    @Override
    public int getItemCount() {
        //返回显示个数
        if(mData!=null){
            return mData.size();
        }
        return 0;
    }

    @SuppressLint("NotifyDataSetChanged")
    public void setDate(List<Album> album_list) {
        if (mData!=null){
            mData.clear();
            mData.addAll(album_list);
        }
        //更新UI
        notifyDataSetChanged();
    }

    public class Innerholder extends RecyclerView.ViewHolder {
        public Innerholder(@NonNull View itemView) {
            super(itemView);
        }

        public void setData(Album album) {
            //找到控件，设置数据
            //设置专辑封面
            ImageView album_cover =itemView.findViewById(R.id.album_cover);
            TextView album_description =itemView.findViewById(R.id.album_description);
            TextView album_title = itemView.findViewById(R.id.album_title);
            TextView play_count =itemView.findViewById(R.id.play_count);
            TextView album_num=itemView.findViewById(R.id.album_num);
            album_title.setText(album.getAlbumTitle());
            album_description.setText(album.getAlbumIntro());
            play_count.setText(album.getPlayCount()+"");
            album_num.setText(album.getIncludeTrackCount()+"");
            Picasso.with(itemView.getContext()).load(album.getCoverUrlLarge()).into(album_cover);
        }
    }
    public void setOnRecommendItemClickListener(OnRecommendItemClick listener){
        this.mItemClickListener=listener;
    }
    public interface OnRecommendItemClick{
        void onItemClick(int position, Album album);
    }
}
